package com.zhxt.web.controller.zhxtapi;

import com.zhxt.common.annotation.Log;
import com.zhxt.common.core.controller.BaseController;
import com.zhxt.common.core.domain.AjaxResult;
import com.zhxt.common.core.page.TableDataInfo;
import com.zhxt.common.enums.BusinessType;
import com.zhxt.common.utils.poi.ExcelUtil;
import com.zhxt.system.domain.LeaderImport;
import com.zhxt.system.domain.SysTjcode;
import com.zhxt.system.domain.ZhxtLeader;
import com.zhxt.system.service.ILeaderSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理系统-干部管理
 */
@RestController
@RequestMapping("/zhxtapi/leader")
public class LeaderAdminController extends BaseController {

    @Autowired
    private ILeaderSerivce iLeaderSerivce;
    /**
     * 查询最后一个月数据
     */
    @GetMapping("/getMaxYearmon")
    public AjaxResult getMaxYearmon(){
        AjaxResult ajax = AjaxResult.success();
        String rYearmon=iLeaderSerivce.getMaxYearmon();
        ajax.put("yearmon",rYearmon==null?"":rYearmon);
        return ajax;
    }

    /**
     * 查询机构列表
     */
    @PostMapping("/getTjcodeList")
    public AjaxResult getTjcodeList(String tjcode){
        AjaxResult ajax = AjaxResult.success();
        List<SysTjcode> sysTjcodeList = iLeaderSerivce.getTjcodeList(tjcode);
        ajax.put("data",sysTjcodeList==null?new ArrayList<>():sysTjcodeList);
        return ajax;
    }

    /**
     * 分页查询干部列表
     */
    @PostMapping("/getLeaderList")
    public TableDataInfo getLeaderList(String yearmon, String tjcode, String name){
        startPage();
        List<ZhxtLeader> zhxtLeaderList=iLeaderSerivce.getLeaderList(yearmon,tjcode,name);
        return getDataTable(zhxtLeaderList);
    }

    /**
     * 导入干部数据
     */
    @Log(title = "pc-导入干部数据", businessType = BusinessType.IMPORT)
    @PostMapping("importLeader")
    public AjaxResult importLeader(MultipartFile file,String yearmon) throws Exception{
        AjaxResult ajax=AjaxResult.success();
        ExcelUtil<LeaderImport> util=new ExcelUtil<LeaderImport>(LeaderImport.class);
        List<LeaderImport> leaderImportList=util.importExcel(file.getInputStream());
        int msg=iLeaderSerivce.importLeader(leaderImportList,yearmon);
        ajax.put("succNum",msg);
        return ajax;
    }



}
